package com.song.composejoke.ui.item

import android.content.Context
import android.content.Intent
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.rememberImagePainter
import coil.transform.RoundedCornersTransformation
import com.song.composejoke.WebActivity
import com.song.composejoke.model.NewsModel

@Composable
fun NewsItem(model: NewsModel, context: Context) {
    Column(content = {
        Card(
            modifier = Modifier
                .wrapContentHeight()
                .fillMaxWidth()
                .padding(8.dp)
                .clickable(enabled = true, onClick = {
                    val intent = Intent(context, WebActivity::class.java)
                    intent.putExtra("url", model.url)
                    intent.putExtra("title", model.title)
                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                    context.startActivity(intent)
                }),
            content = {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Image(
                        painter = rememberImagePainter(data = model.thumbnail_pic_s,
                            builder = {
                                transformations(RoundedCornersTransformation(10.0f))
                            }),
                        contentDescription = null,
                        modifier = Modifier
                            .padding(horizontal = 10.dp)
                            .size(60.dp)
                    )
                    Column() {
                        Text(
                            model.title,
                            modifier = Modifier
                                .wrapContentHeight()
                                .fillMaxWidth()
                                .padding(8.dp),
                            fontSize = 16.sp,
                            fontWeight = FontWeight.Bold
                        )
                        Text(
                            "来源：" + model.author_name,
                            modifier = Modifier
                                .wrapContentHeight()
                                .fillMaxWidth()
                                .padding(8.dp),
                            fontSize = 14.sp,
                        )
                    }
                }
            },
            backgroundColor = Color.White,
            elevation = 2.dp,
        )
    })
}